package com.example.myprojectoffeixue;

/**
 * Created by ASUS on 2016/11/18.
 */

import android.app.Dialog;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.alibaba.fastjson.JSON;
import org.java_websocket.client.WebSocketClient;
import java.util.ArrayList;
public class MyDialog extends Dialog {
    public boolean flag=true;
    ListView list_gateway;
    public String add_mac;
    private ImageView iv_dialogexit;
    public WebSocketClient wsc;
    public ArrayList<String> mac;
    public TextView tv;
    public TextView tv_start;
    int i =600;
    public ImageView iv;
    private static int default_width = 160; //默认宽度
    private static int default_height = 120;//默认高度

    Handler hand=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
                iv.clearAnimation();
                tv_start.setText("开始");
                flag=false;
                tv.setText(msg.what+"");
                i=600;
            }else{
            tv.setText(msg.what+"");}
            super.handleMessage(msg);
        }
    };

    public MyDialog(final Context context, View layout, int style, final ArrayList<String> mac, final TextView tv_start, final TextView tv_select,final TextView tv_select2, final MyWebSocketClient wsc, final TextView tv, final ImageView iv, final Animation la) {
        this(context, default_width, default_height, layout, style);
        this.wsc = wsc;
        this.tv = tv;
        this.iv = iv;
        this.mac = mac;
        this.tv_start=tv_start;
        AddSheBeiActivity ad = (AddSheBeiActivity) context;
        iv_dialogexit = (ImageView) layout.findViewById(R.id.exit);
        iv_dialogexit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDialog.this.dismiss();
            }
        });
        add_mac=tv_select.getText().toString();
        tv_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("开始".equals(tv_start.getText().toString())) {
                    if(!"".equals(add_mac)){
                    flag=true;
                    String s = "01";
                    String cmd = "1100";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.status = s;
                    k.cmd = cmd;
                    k.mac = add_mac;
                    String j = JSON.toJSONString(k);
                    wsc.send(j);
                        new Thread() {
                            @Override
                            public void run() {
                                for (;i > 0; i--) {
                                    if (flag) {
                                        try {
                                            sleep(100);
                                            if(i%10==0){
                                            hand.sendEmptyMessage(i/10);}
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        return;
                                    }
                                }
                                super.run();
                            }
                        }.start();
                    tv_start.setText("暂停");
                    tv.setVisibility(View.VISIBLE);
                    iv.setVisibility(View.VISIBLE);
                    iv.clearAnimation();
                    LinearInterpolator lin = new LinearInterpolator();
                    la.setInterpolator(lin);
                    if (la != null) {
                        iv.startAnimation(la);
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                iv.setVisibility(View.VISIBLE);
                            }
                        }, 100);
                    }}else{
                        ToastUtils.showToast(context,"请先选择网关");
                    }

                } else if ("暂停".equals(tv_start.getText().toString())) {
                    flag=false;
                    String s = "00";
                    String cmd = "1100";
                    Kuaiguanutil k = new Kuaiguanutil();
                    k.status = s;
                    k.cmd = cmd;
                    k.mac = add_mac;
                    String j = JSON.toJSONString(k);
                    wsc.send(j);
                    iv.clearAnimation();
                    tv.setVisibility(View.VISIBLE);
                    tv.clearAnimation();
                    tv_start.setText("开始");
                }
            }
        });
        list_gateway = (ListView) layout.findViewById(R.id.list_gateway);
        adapter_dialog ap = new adapter_dialog(mac, context);
        list_gateway.setAdapter(ap);
        list_gateway.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                add_mac = mac.get(position);
                if(add_mac!=null){
                MyDialog.this.dismiss();
                tv_select.setText(add_mac);
                tv_select2.setText("已选择网关");
                tv_start.setClickable(true);
                tv_start.setBackgroundResource(R.drawable.bancolor);}
            }
        });

    }


    public MyDialog(Context context, int width, int height, View layout, int style) {
        super(context, style);
        setContentView(layout);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }


}
